package servicesImp.dictionary;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import services.dictionary.CertificateTypeServices;
import services.dictionary.ItemTypeServices;
import wt.dao.dictionary.CertificateTypeMapper;
import wt.entity.dictionary.CertificateType;
import wt.entity.dictionary.CertificateTypeExample;
import wt.entity.dictionary.ItemType;
import wt.staticData.TableName;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CertificateTypeServicesImp extends IBaseServicesWithoutBLOBsImpl<CertificateType, CertificateTypeExample> implements CertificateTypeServices {
    @Autowired
    CertificateTypeMapper certificateTypeMapper;
    @Autowired
    ItemTypeServices ItemTypeServices;

    @Override
    public IBaseMapperWithoutBLOBs<CertificateType, CertificateTypeExample> getMapper() {
        return this.certificateTypeMapper;
    }



    @Override
    public String getTableName() {
        return TableName.DIC_CERTIFICATE_TYPE;
    }

    public void lazyLoadItemType(List<CertificateType> certificateTypeList) throws Exception {
            Map<String, ItemType> map = new HashMap<String, ItemType>();
            for (CertificateType certificateType : certificateTypeList) {
                if (!map.containsKey(certificateType.getItemId())) {
                    map.put(certificateType.getItemId(), ItemTypeServices.selectByPrimaryKey(certificateType.getItemId()));
                }
                certificateType.setItemType(map.get(certificateType.getItemId()));
            }
    }

    public CertificateType selectByName(String name) throws Exception {
        CertificateTypeExample certificateTypeExample = new CertificateTypeExample();
        certificateTypeExample.createCriteria().andNameEqualTo(name);
        List<CertificateType> certificateTypeList=selectByExample(certificateTypeExample);
        if (certificateTypeList.isEmpty()){
            return null;
        }else{
          return   certificateTypeList.get(0);
        }
    }
}
